# Linux insmod(8), modprobe(8) and modinfo(8) completion. This completes on a
# list of all available modules for the version of the kernel currently
# running.


comp_include _filedir _get_cword _modules


_insmod()
{
    local cur prev modpath

    COMPREPLY=()
    cur=`_get_cword`
    prev=${COMP_WORDS[COMP_CWORD-1]}

    # behave like lsmod for modprobe -r
    if [ $1 = "modprobe" ] &&
       [ "${COMP_WORDS[1]}" = "-r" ]; then
        COMPREPLY=( $( /sbin/lsmod | \
                awk '{if (NR != 1 && $1 ~ /^'$cur'/) print $1}' ) )
        return 0
    fi

    # do filename completion if we're giving a path to a module
    if [[ "$cur" == */* ]]; then
        _filedir '@(?(k)o?(.gz))'
        return 0
    fi

    if [ $COMP_CWORD -gt 1 ] && 
       [[ "${COMP_WORDS[COMP_CWORD-1]}" != -* ]]; then
        # do module parameter completion
        COMPREPLY=( $( /sbin/modinfo -p ${COMP_WORDS[1]} 2>/dev/null | \
               awk '{if ($1 ~ /^parm:/ && $2 ~ /^'$cur'/) { print $2 } \
            else if ($1 !~ /:/ && $1 ~ /^'$cur'/) { print $1 }}' ) )
    else
        _modules $(uname -r)
    fi

    return 0
} # _insmod()
